<?php

class User_Table_Model_User extends Database_Model_Item implements Nuo_User_Interface
{

	/**
	 * @var string
	 */
	protected $_itemType = 'user';

	/**
	 * @var string
	 */
	protected $_pk = 'user_id';
	
	/**
	 * Keep Authorization object
	 * @access  protected
	 * @var     Nuo_Acl_Interface
	 */
	protected $_acl = NULL;

	/**
	 * Get display name
	 * @access   public
	 * @return   string
	 */
	function getTitle()
	{
		return $this -> full_name;
	}

	/**
	 * Get viewer user id
	 * @access    public
	 * @return   int
	 */
	function getId()
	{
		return $this -> user_id;
	}

	/**
	 * Is User ?
	 * @access   public
	 * @return   bool
	 */
	function isMember()
	{
		return true;
	}
	
	/**
	 * Is Guest ?
	 * @access   public
	 * @return   bool
	 */
	function isGuest()
	{
		return false;
	}

	/**
	 * Set new permission value associate with action
	 * WARNING:
	 * + Do not store this value to database
	 * + To store new value, use permission api instead
	 * @access  public
	 * @param   string  $action
	 * @param   mixed   $value
	 * @return  Nuo_Permission
	 */
	function setAllow($action, $value)
	{
		return $this -> getAcl() -> setAllow($action, $value);
	}

	/**
	 * Get permission of this action
	 * @access public  int
	 * @param  string  $action
	 * @return mixed
	 */
	function allow($action)
	{
		return $this -> getAcl() -> allow($action);
	}

	/**
	 * Required user to allow to action.
	 * + If user has authorized to do "action", return true
	 * + If user has no permission to pass "action", redirect user to login required
	 * or permission deined page.
	 * + If you want to get permission instead of required user, please use
	 * ":allow()" instead of this method.
	 * @access  public
	 * @param   string   $action
	 * @param   int      $def
	 * @return  void
	 */
	function required($action)
	{
		return $this -> getAcl() -> required($action);
	}

	/**
	 * Get Acl permission for current user
	 * @access  public
	 * @return  Nuo_Acl_User
	 */
	function getAcl()
	{
		if (NULL == $this -> _acl)
		{
			$this -> _acl = Nuo_Acl_User::factory($this->getId());
		}
		return $this -> _acl;
	}

	/**
	 * @access   public
	 * @param    Object  $object
	 * @return   bool
	 */
	function isOwner($object)
	{
		return $object -> getItemId() == $this -> getItemId();
	}

	/**
	 * Get Permission
	 * @access    public
	 * @param     string  $action
	 * @return    mixed
	 */
	function getPerm($action)
	{

	}

	/**
	 * @return string
	 */
	function getCardhoverUrl()
	{
		return Nuo::getUrl('user/cardhover?user_id=' . $this -> getId());
	}

	/**
	 * @return owner
	 */
	function getOwner()
	{
		return $this;
	}

	/**
	 * @return string
	 */
	function getAlias()
	{
		if ($this -> username)
		{

			return $this -> username;
		}

		return 'user-' . $this -> getId();
	}

	/**
	 * get role id
	 */
	function getRoleId()
	{
		return $this -> role_id;
	}

	/**
	 * process to anchor
	 * @return string
	 */
	public function getUrl($p = '', $q = NULL)
	{
		if ($this -> username)
		{
			$p = Nuo::getUrl($this -> username) . '/' . trim($p, '/');
			if ($q)
			{
				$p .= '?' . http_build_query($q);
			}
		}
		else
		{
			$q['user_id'] = $this -> getId();
			$p = Nuo::getUrl('user/profile') . '?' . http_build_query($q, NULL, '&');
		}
		return $p;

	}

	/**
	 * check is password
	 * @param string $password
	 * @return bool
	 */
	function isPassword($password)
	{
		return sha1(sha1($password) . sha1($this -> password_salt)) == $this -> password_hash;
	}

	/**
	 * this password still not affected until call saved!
	 * @return User_Table_Model_User
	 */
	function setPassword($password = NULL)
	{
		if (NULL == $password)
		{
			$password = get_random_text(4);
		}

		/**
		 * @var string
		 */
		$this -> password_salt = get_random_text(4);

		/**
		 * @var string
		 */
		$this -> password_hash = sha1(sha1($password) . sha1($this -> password_salt));
		return $this;
	}

	/**
	 * login with current user id
	 * @return User
	 */
	public function login()
	{
		$this -> login_ip = Nuo::getRequest()->getClientIp();
		$this -> login_at = time();
		$_SESSION['user_id'] = $this -> getId();
		return $this;
	}

	/**
	 * login with current user id
	 * @return User
	 */
	public function logout()
	{
		if (isset($_SESSION['user_id']))
		{
			unset($_SESSION['user_id']);
		}
		return $this;
	}

	protected function _update()
	{
		if (!$this -> username)
		{
			$this -> username = NULL;
		}
	}

	protected function _insert()
	{

		if (!$this -> role_id)
		{
			$this -> role_id = Nuo::getConfig('user', 'roleId');
		}

		$this -> updated_ip = $this -> created_ip = Nuo::getRequest()->getClientIp();
		$this -> updated_at = $this -> created_at = time();
	}

	/**
	 * privacy table
	 */
	function getProxy()
	{
		return $this;
	}

}
